<%
/*
 * Copyright (c) 2011 Metrum Research Group LLC. All Rights Reserved.
 *
 * You may use this software under the terms of GPL v3 (http://www.gnu.org/licenses/gpl.html).
 *
 * METRUM RESEARCH GROUP LLC MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. METRUM RESEARCH GROUP LLC SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */
%>
<%@page pageEncoding="UTF-8"%>
<%@ page language="java" contentType="text/html" %>
<%@ page import="sumo.SumoPropertyManager" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html> 
<head>
	<META http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>Sumo Monitor</title>
	<%
	response.setHeader("Cache-Control","no-store"); //HTTP 1.1
	response.setHeader("Pragma","no-cache"); //HTTP 1.0
	response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
	%>

<script type="text/javascript" src="jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="jquery.textarearesizer.compressed.js"></script>

<script type="text/javascript">
$(document).ready(function() {
$('textarea.resizable:not(.processed)').TextAreaResizer();
$('iframe.resizable:not(.processed)').TextAreaResizer();
scrollIt();
document.forms.singleLineForm.theUserInput.focus();
});
</script>

<style type="text/css">
/*div.grippie {
background:#EEEEEE url(img/grippie.png) no-repeat scroll center 2px;
border-color:#DDDDDD;
border-style:solid;
border-width:0pt 1px 1px;
cursor:s-resize;
height:9px;
}
*/
.resizable-textarea textarea {
font-family: monospace;
background-color: navy;
border: black 2px solid;
color: white;
font-weight: normal;
display:block;
margin-bottom:0pt;
width:95%;
height: 20%;
}

</style>
	
<script type="text/javascript" language="javascript">

   var uc_http_request = false;
   var gt_http_request = false;
   var gi_http_request = false;
   	   
   function makePOSTRequest(url, parameters) {
      uc_http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...      
         uc_http_request = new XMLHttpRequest();  	         
      } else if (window.ActiveXObject) { // IE
         try {
            uc_http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               uc_http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
  	      
      if (!uc_http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      uc_http_request.onreadystatechange = alertContents;
      uc_http_request.open('POST', url, true);
      uc_http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      uc_http_request.setRequestHeader("Content-length", parameters.length);
      uc_http_request.setRequestHeader("Connection", "close");
      uc_http_request.send(parameters); 	      
   }

   function alertContents() {
      if (uc_http_request.readyState == 4) {
         if (uc_http_request.status == 200) {
            result = uc_http_request.responseText; 
            document.getElementById("theUserInput").value = '';
            document.getElementById("theUserMLInput").value = '';      
         } 
      }
   }
   
   function post(obj) { 
      var poststr = "theUserInput=" + encodeURIComponent(document.getElementById("theUserInput").value);
      makePOSTRequest('UserCommand', poststr);		      
   }

   function postML(obj) { 
      var poststr = "theUserInput=" + encodeURIComponent(document.getElementById("theUserMLInput").value);
      makePOSTRequest('UserCommand', poststr);		      
   }
      
   function scrollIt() {
		var obj=document.getElementById('bigtext');			
		obj.scrollTop=obj.scrollHeight;
   }

  <% 
   int sessionrefresh = SumoPropertyManager.getConfig().getInt("sessionpage.sessionrefresh");
   %>

   setInterval ("refreshSessionWindow()", <%= sessionrefresh %>);
	
   function refreshSessionWindow() { 
          makeGETRequest('SessionOutput', 'test');		      
   }
   
   function makeGETRequest(url, parameters) {  
      gt_http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...      
         gt_http_request = new XMLHttpRequest();  	         
      } else if (window.ActiveXObject) { // IE
         try {
            gt_http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               gt_http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
  	      
      if (!gt_http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      gt_http_request.onreadystatechange = loadSessionWindow;	      
	  gt_http_request.open('GET', url, true);
	  gt_http_request.setRequestHeader("Content-type", "text/xml");
	  gt_http_request.setRequestHeader("Content-length", parameters.length);
	  gt_http_request.setRequestHeader("Connection", "close");
	  gt_http_request.send(null);	  
   }
   
   function loadSessionWindow() {	   
      if (gt_http_request.readyState == 4) {
         if (gt_http_request.status == 200) {
            result = gt_http_request.responseText;
	    // Check for NO_CHANGE response
	    if(result != 'NO_CHANGE'){
		// If there was a change, refresh the textarea and scroll it
		var obj=document.getElementById('bigtext').value = result;
		scrollIt();
	    }	            
         } 
      }
   }
   
		 	   	 	      	
</script>
</head>

<body leftmargin="20"> 
<br/>
<table border="1" bgcolor="#FFFFFF" width="400">
<tr>
<td>
<textarea id='bigtext' name='bigtext' rows="15" cols="100" wrap="virtual" style="width: 100%;" class="rInput">
<c:out value='${Output}'/> 
</textarea>
</td>
</tr>
</table>

<br>

<table width="400">
<tr>
<form action="javascript:post(document.getElementById('singleLineForm'));" 
	name="singleLineForm" id="singleLineForm">
<td align="left">Single line input:&nbsp;</td>
<td align="center">
<input type="text" id='theUserInput' name='theUserInput' class="rInput"  
	style="width: 310px;" selected="true">
</td>
<td align="right"> 
&nbsp; 
</td>
</form>
</tr>
<tr>
<form action="javascript:postML(document.getElementById('multiLineForm'));" 
	name="multiLineForm" id="multiLineForm">

<td align="left">
<table>
<tr>
<td align="left">Multi-line input:&nbsp;</td>
</tr>
<tr>
<td>
<input type="submit" name="button" value="Evaluate" class="sumobutton">
</td>
</tr>
</table>
</td>

<td align="center">
<textarea id='theUserMLInput' name='theUserMLInput' rows="3" cols="100" wrap="virtual"
	style="width: 310px;" class="rInput">
</textarea>
</td>
<td align="right">&nbsp;</td>
</form>
</tr>
</table> 
</center>
</body> 
</html> 
